@file:JvmName("Clause")

package dsl

import ast.expr.SqlSubQueryPredicate
import expr.*
import query.select.Select
import query.select.SelectQuery
import query.select.ValuesSelect

infix fun String.alias(alias: String): QueryColumn {
    return QueryColumn(this, alias)
}

fun column(column: String): QueryColumn {
    return QueryColumn(column, null)
}

fun column(column: String, alias: String): QueryColumn {
    return QueryColumn(column, alias)
}

fun allColumn(): QueryAllColumn {
    return QueryAllColumn(null)
}

@JvmName("value")
fun <T> const(value: T): QueryConst<T> {
    return QueryConst(value)
}

@JvmName("value")
fun <T> const(value: T, alias: String): QueryConst<T> {
    return QueryConst(value, alias)
}

fun <T> case(vararg conditions: CaseBranch<T>): QueryCase<T> {
    return QueryCase(conditions.toList())
}

fun exists(select: SelectQuery): QuerySubQueryPredicate {
    return QuerySubQueryPredicate(select, SqlSubQueryPredicate.EXISTS)
}

fun notExists(select: SelectQuery): QuerySubQueryPredicate {
    return QuerySubQueryPredicate(select, SqlSubQueryPredicate.NOT_EXISTS)
}

fun all(select: SelectQuery): QuerySubQueryPredicate {
    return QuerySubQueryPredicate(select, SqlSubQueryPredicate.ALL)
}

fun any(select: SelectQuery): QuerySubQueryPredicate {
    return QuerySubQueryPredicate(select, SqlSubQueryPredicate.ANY)
}

fun values(values: List<List<Any>>): ValuesSelect {
    val valuesSelect = ValuesSelect()

    values.forEach {
        valuesSelect.addRow(it)
    }

    return valuesSelect
}

fun values(vararg value: List<Any>): ValuesSelect {
    return values(value.toList())
}